package com.template;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.template.utils.TStringUtils;

public class TController {

	public static void createXMLFile(String tableName, String packageName,List<TableBean>columnList,List<TableBean>priList, List<TableBean>noPriList, String folderName, String path) {
		String className = TStringUtils.tableName2ClassName(tableName);
	    String attrName = TStringUtils.columnName2Attr(tableName);
	    String str = "";
	    str+="package "+packageName+".controller;\r\n";
	    str+="import java.util.List;\r\n";
	    str+="import org.springframework.beans.factory.annotation.Autowired;\r\n";
	    str+="import org.springframework.stereotype.Controller;\r\n";
	    str+="import org.springframework.ui.Model;\r\n";
	    str+="import org.springframework.web.bind.annotation.GetMapping;\r\n";
	    str+="import org.springframework.web.bind.annotation.PathVariable;\r\n";
	    str+="import org.springframework.web.bind.annotation.RequestMapping;\r\n";
	    str+="import org.springframework.web.bind.annotation.RequestMethod;\r\n";
	    str+="import org.springframework.web.bind.annotation.ResponseBody;\r\n";
	    str+="import com.alibaba.fastjson.JSONObject;\r\n";
	    str+="import "+packageName+".controller.base.BaseController;\r\n";
	    str+="import "+packageName+".entity.ResponseModel;\r\n";
	    str+="import "+packageName+".entity.ResultEnum;\r\n";
	    str+="import "+packageName+".service.I"+className+"Service;\r\n";
	    str+="import "+packageName+".util.Jurisdiction;\r\n";
	    str+="import "+packageName+".util.ParameterMap;\r\n\r\n";
	    str+="@Controller\r\n";
	    str+="@RequestMapping(\"/"+attrName+"\")\r\n";
	    str+="public class "+className+"Controller extends BaseController {\r\n";
	    str+="   private String qxurl = \""+attrName+"/list\";\r\n";
	    str+="   @Autowired\r\n";
        str+="   private I"+className+"Service i"+attrName+"Service;\r\n";
	    str+="   /**\r\n";
        str+="     * 获取列表\r\n";
        str+="     * @return\r\n";
        str+="   */\r\n";
        str+="   @GetMapping(\"/listjson\")\r\n";
        str+="   @ResponseBody\r\n";
        str+="   public Object listjson(){\r\n";
        str+="   List<ParameterMap> list = i"+attrName+"Service.get"+className+"List();\r\n";
        str+="  return JSONObject.toJSONString(list);\r\n";
        str+="  }\r\n\r\n\r\n";
        str+="  /**\r\n";
        str+="   * 获取列表2\r\n";
        str+="    * @return\r\n";
	    str+="   */\r\n";
        str+="   @RequestMapping(value=\"/list\",method=RequestMethod.GET)\r\n";
	    str+="   public Object list(Model model){\r\n";
	    str+="    model.addAttribute(\""+attrName+"s\", i"+attrName+"Service.get"+className+"List());\r\n";
        str+="    return \"page/"+attrName+"/list\";\r\n";
	    str+="   }\r\n\r\n\r\n";
        str+="   /**\r\n";
        str+="   * 更改\r\n";
        str+="   * @return\r\n";
        str+="    */\r\n";
        str+="   @RequestMapping(value=\"/edit\",method=RequestMethod.POST)\r\n";
        str+="   @ResponseBody\r\n";
        str+="   public Object edit(){\r\n";
        str+="   if(!Jurisdiction.buttonJurisdiction(qxurl, \"edit\",this.getSession())){return ResponseModel.getModel(ResultEnum.NOT_AUTH, null);} //校验权限\r\n";
	    str+="   return i"+attrName+"Service.edit(this.getParameterMap());\r\n";
        str+="  }\r\n\r\n";
        str+="  /**\r\n";
        str+="   * 添加\r\n";
        str+="   * @return\r\n";
        str+="   */\r\n";
        str+="   @RequestMapping(value=\"/add\",method=RequestMethod.POST)\r\n";
        str+="   @ResponseBody\r\n";
        str+="   public Object add(){\r\n";
        str+="     if(!Jurisdiction.buttonJurisdiction(qxurl, \"add\",this.getSession())){return ResponseModel.getModel(ResultEnum.NOT_AUTH, null);} //校验权限\r\n";
        str+="     return i"+attrName+"Service.add(this.getParameterMap());\r\n";
        str+="    }\r\n\r\n";
        str+="   /**\r\n";
        str+="    * 删除\r\n";
        str+="    * @return\r\n";
        str+="    */\r\n";
        str+="   @RequestMapping(value=\"/del/{id}\",method=RequestMethod.GET)\r\n";
        str+="   @ResponseBody\r\n";
        str+="   public Object del(@PathVariable(\"id\") int id){\r\n";
        str+="   if(!Jurisdiction.buttonJurisdiction(qxurl, \"del\",this.getSession())){return ResponseModel.getModel(ResultEnum.NOT_AUTH, null);} //校验权限\r\n";
        str+="   return i"+attrName+"Service.del(id);\r\n";
        str+="   }\r\n";
        str+="}\r\n";

	    
		try {
			String allPath = path+"\\"+className+"Controller.java";
			OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(allPath),"UTF-8");
			output.write(str);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
